<?php
class mMemcache {
	private $mmc = NULL;
    private $group = ''; 
    private $ver = 0;
    protected $_memcached_conf = array( 
        'default_host' => '127.0.0.1', 
        'default_port' => 11211, 
        'default_weight' => 1, 
    ); 

    public function __construct() { 

        $this->mmc = new Memcache($this->_memcached_conf); 
        $this->mmc->ConnectMemcache('127.0.0.1', '11211'); 
    }
  
  //实现链接memccache功能
  	private function   ConnectMemcache($server, $port){
		$sessionHandler=ini_get('session.save_handler'); //从ini中获取配置信息
		if($sessionHandler=='memcache'){
			$sessionPath=ini_get('session.save_path'); //  //192.168.8.177:11211
			$sessionPath=str_replace('//','',$sessionPath);//   192.168.8.177:11211
			$sessionHost=explode(':',$sessionPath);
			$this->server=$sessionHost[1];
			$this->port=$sessionHost[2];
		}else{
			$this->server=$server;
			$this->port=$port;
		}
		
		$this->_memcacheHnader=memcache_connect($this->server,$this->port);
		if($this->_memcacheHnader){
			return true;
		}else{
			return false;
		}
		
		
	}
  
 
   //获得memcache的版本信息
   public function version(){
		return $this->mmc->getVersion();
   }

 	//读取缓存
    public function get($key) {
  		return $this->mmc->get($key);
    }
 
 	//设置缓存
    public function set($key,$value,$expire = 1800) {
  		return $this->mmc->set($key, $value, $expire);
    }
 

	//添加缓存
   public function add($key, $value, $expire = 1800) {
	  if(!get($key)){
		 return $this->mmc->add($key, $value, $expire);
	  }else{
		 echo "设置失败，该键值被已被注册";
	   return false;
	  }
   }
 
   //替换缓存
 public function replace($key, $value, $expire = 1800){
   return $this->mmc->replace($key, $value);
 }
 
 //自增1
  public function inc($key, $value = 1) {
	 return $this->mmc->increment($key, $value);
  }
 
 //自减1
 public function des($key, $value = 1) {
  	 return $this->mmc->decrement($key, $value);
  }
 
 //删除
 public function del($key) {
  	return $this->mmc->delete($key);
 }
 
 //全部清空
  public function clean() {
	return  $this->mmc->flush(); 
  }
 
 //关闭缓存
 public function close() {
  return $this->mmc->close();
 }
 
}
?>